# -*- coding: utf-8 -*-
__author__ = 'liubei'

from email.mime.multipart import MIMEMultipart
import smtplib
import sys
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase

if __name__ == "__main__":
    args = sys.argv[1:]

    #创建一个带附件的实例
    msg = MIMEMultipart()

    #构造附件1
    # att1 = MIMEText(open(args[0], 'rb').read(), 'base64', 'gb2312')
    # att1["Content-Type"] = 'application/octet-stream'
    # att1["Content-Disposition"] = 'attachment; filename="export.zip"'#这里的filename可以任意写，写什么名字，邮件中显示什么名字
    # msg.attach(att1)

    # 邮件正文是MIMEText:
    msg.attach(MIMEText(args[2], 'plain', 'utf-8'))

    # 添加附件就是加上一个MIMEBase，从本地读取一个图片:
    # with open(args[0], 'rb') as f:
    #     # 设置附件的MIME和文件名，这里是png类型:
    #     mime = MIMEBase('image', 'png', filename='test.png')
    #     # 加上必要的头信息:
    #     mime.add_header('Content-Disposition', 'attachment', filename='test.png')
    #     mime.add_header('Content-ID', '<0>')
    #     mime.add_header('X-Attachment-Id', '0')
    #     # 把附件的内容读进来:
    #     mime.set_payload(f.read())
    #     # 用Base64编码:
    #     encoders.encode_base64(mime)
    #     # 添加到MIMEMultipart:
    #     msg.attach(mime)

    #加邮件头
    msg['to'] = args[1]
    msg['from'] = '18018626781@163.com'
    msg['subject'] = '报表'
    # msg['describe'] = args[2]
    #发送邮件
    try:
        server = smtplib.SMTP()
        server.connect('smtp.163.com')
        server.login('m18018626781@163.com', 'pmweptofycitkasf')#XXX为用户名，XXXXX为密码
        server.sendmail(msg['from'], msg['to'], msg.as_string())
        server.quit()
        print '发送成功'
    except Exception, e:
        print str(e)